﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfLookup_CS
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private OrderDataSet OrderData = new OrderDataSet();
        private OrderDataSetTableAdapters.OrdersTableAdapter taOrder = new OrderDataSetTableAdapters.OrdersTableAdapter();
        private OrderDataSetTableAdapters.TableAdapterManager taManager = new OrderDataSetTableAdapters.TableAdapterManager();
        private CustomerLookupDataSet.CustomerDataTable CustomerLookup = new CustomerLookupDataSet.CustomerDataTable();
        private CollectionView View;

        private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            CustomerLookupDataSetTableAdapters.CustomerTableAdapter taCustomer = new CustomerLookupDataSetTableAdapters.CustomerTableAdapter();
            taCustomer.Fill(this.CustomerLookup);
            this.taOrder.Fill(this.OrderData.Orders);
            this.taManager.OrdersTableAdapter = this.taOrder;
            this.DataContext = this.OrderData.Orders;
			this.ComboBox1.ItemsSource = new CustomerLookupDataSet.CustomerDataTable();
            this.View =(CollectionView) (CollectionViewSource.GetDefaultView(this.OrderData.Orders));
        }

        private void btnAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            OrderDataSet.OrdersRow row = this.OrderData.Orders.NewOrdersRow();
            row.CustomerID = 0;
            this.OrderData.Orders.AddOrdersRow(row);
            this.View.MoveCurrentToLast();
        }

        private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (this.OrderData.HasChanges())
                {
                    if ((this.taManager.UpdateAll(this.OrderData) > 0))
                    {
                        MessageBox.Show("Saved");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
       

    }
}
